package model

type City struct {
    base            `json:"-" field:"-"`
    Id   int        `field:"id"`
    Code int        `field:"code"`
    Name string     `field:"name"`
}

func NewCity() *City {
    m := new(City)
    m.init("city")
    return m
}

func (this *City) Search(name string) (list []*City, err error) {
    cond := this.DB.NewCondition()
    cond.SetFilterEx("code", ">", 10000)
    cond.SetFilterEx("name", "like", "%" + name + "%")
    if r, e := this.DB.SetCondition(cond).FindAll(); e != nil {
        err = e
        return
    } else {
        r.Scan(&list)
    }
    return
}

func (this *City) InitByCode(code int) error {
    cond := this.DB.NewCondition()
    cond.SetFilter("code", code)
    res, err := this.DB.SetCondition(cond).FindOne()
    if res != nil {
        res.Scan(this)
    }
    return err
}
